package com.danger.Activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.danger.Model.bean.ObjectInfo;
import com.danger.Presenter.MyObjectInfoDetailPresenter;
import com.danger.Presenter.MyObjectInfoDetailPresenterImpl;
import com.danger.Util.ImageLoaderUtil;
import com.danger.View.MyObjectDetailView;

public class MyObjectInfoDetailActivity extends AppCompatActivity implements MyObjectDetailView {

    private CollapsingToolbarLayout toolbar_layout;
    private TextView myobjectinfo_detail_title_textView;
    private TextView myobjectinfo_detai_title;
    private TextView myobjectinfo_detail_category_textView;
    private TextView myobjectinfo_detail_category;
    private TextView myobjectinfo_detail_describe_textView;
    private TextView myobjectinfo_detail_describe;
    private TextView myobjectinfo_detail_address_textView;
    private TextView myobjectinfo_detail_address;
    private TextView myobjectinfo_detail_time_textView;
    private TextView myobjectinfo_detail_time;
    private TextView myobjectinfo_detail_name_textView;
    private TextView myobjectinfo_detail_phone_textView;
    private TextView myobjectinfo_detail_qq_textView;
    private Intent intent;
    private Toolbar toolbar;
    private Handler handler = new Handler();
    private String id;
    private ProgressDialog dialog;
    private MyObjectInfoDetailPresenter presenter;
    private ObjectInfo objectInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_objectinfo_detail);

        initView();
        initEvent();

    }

    private void initView() {
        toolbar_layout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        myobjectinfo_detail_title_textView = (TextView) findViewById(R.id.myobjectinfo_detail_title_textView);
        myobjectinfo_detai_title = (TextView) findViewById(R.id.myobjectinfo_detai_title);
        myobjectinfo_detail_category_textView = (TextView) findViewById(R.id.myobjectinfo_detail_category_textView);
        myobjectinfo_detail_category = (TextView) findViewById(R.id.myobjectinfo_detail_category);
        myobjectinfo_detail_describe_textView = (TextView) findViewById(R.id.myobjectinfo_detail_describe_textView);
        myobjectinfo_detail_describe = (TextView) findViewById(R.id.myobjectinfo_detail_describe);
        myobjectinfo_detail_address_textView = (TextView) findViewById(R.id.myobjectinfo_detail_address_textView);
        myobjectinfo_detail_address = (TextView) findViewById(R.id.myobjectinfo_detail_address);
        myobjectinfo_detail_time_textView = (TextView) findViewById(R.id.myobjectinfo_detail_time_textView);
        myobjectinfo_detail_time = (TextView) findViewById(R.id.myobjectinfo_detail_time);
        myobjectinfo_detail_name_textView = (TextView) findViewById(R.id.myobjectinfo_detail_name_textView);
        myobjectinfo_detail_phone_textView = (TextView) findViewById(R.id.myobjectinfo_detail_phone_textView);
        myobjectinfo_detail_qq_textView = (TextView) findViewById(R.id.myobjectinfo_detail_qq_textView);
        dialog = new ProgressDialog(this);
        presenter = new MyObjectInfoDetailPresenterImpl(this, this);
        intent = getIntent();
        toolbar = (Toolbar) findViewById(R.id.myobjectinfo_detail_toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        if (intent != null) {
            objectInfo = (ObjectInfo) intent.getSerializableExtra("objectInfo");
            String url = objectInfo.getPhoto().toString();
            if (url == "" || url == null) {
                toolbar_layout.setBackgroundResource(R.drawable.black);
            } else {
                new ImageLoaderUtil(toolbar_layout, url, this).loadLayoutBackGround();
            }
            if (objectInfo.isLost()) {
                toolbar.setTitle("失物详情页");
                myobjectinfo_detai_title.setText("失物名称：  ");
                myobjectinfo_detail_category.setText("失物类别：  ");
                myobjectinfo_detail_describe.setText("失物描述：  ");
                myobjectinfo_detail_address.setText("丢失地点：  ");
                myobjectinfo_detail_time.setText("丢失时间：  ");
            } else {
                toolbar.setTitle("招领详情页");
                myobjectinfo_detai_title.setText("拾物名称：  ");
                myobjectinfo_detail_category.setText("拾物类别：  ");
                myobjectinfo_detail_describe.setText("拾物描述：  ");
                myobjectinfo_detail_address.setText("拾物地点：  ");
                myobjectinfo_detail_time.setText("拾物时间：  ");
            }
            id = objectInfo.getObjectId();
            myobjectinfo_detail_title_textView.setText(objectInfo.getTitle());
            myobjectinfo_detail_category_textView.setText(objectInfo.getCategory());
            myobjectinfo_detail_describe_textView.setText(objectInfo.getDescribe());
            myobjectinfo_detail_address_textView.setText(objectInfo.getAddress());
            myobjectinfo_detail_time_textView.setText(objectInfo.getDate().getDate().toString());
            myobjectinfo_detail_name_textView.setText(objectInfo.getName());
            myobjectinfo_detail_phone_textView.setText(objectInfo.getPhone());
            myobjectinfo_detail_qq_textView.setText(objectInfo.getQq());
        }
        setSupportActionBar(toolbar);
    }


    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.my_detail_share:
                        presenter.share();
                        break;
                    case R.id.my_detail_delete:
                        AlertDialog.Builder builder = new AlertDialog.Builder(MyObjectInfoDetailActivity.this);
                        builder.setTitle("删除提示");
                        builder.setMessage("确定删除？");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                presenter.delete(id);
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub

                            }
                        });
                        AlertDialog alertDialog = builder.create();
                        alertDialog.show();
                        break;
                    case R.id.my_detail_modify:
                        presenter.toModify();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public void showDialog(String code) {
        switch (code) {
            case "delete":
                dialog.setMessage("正在删除...");
                break;
            case "share":
                dialog.setMessage("正在分享...");
                break;
            default:
                break;
        }
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showSuccess(String s, ObjectInfo object) {
        if (object != null) {
            objectInfo = object;
        }
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void back() {
        finish();
    }

    @Override
    public void toModify() {
        Intent intent = new Intent(MyObjectInfoDetailActivity.this, ModifyActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("objectInfo", objectInfo);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_my_objectinfo_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public String getShareContent() {
        String title = myobjectinfo_detai_title.getText().toString().trim() + objectInfo.getTitle().toString() + "\n";
        String category = myobjectinfo_detail_category.getText().toString().trim() + objectInfo.getCategory().toString() + "\n";
        String describe = myobjectinfo_detail_describe.getText().toString().trim() + objectInfo.getDescribe().toString() + "\n";
        String address = myobjectinfo_detail_address.getText().toString().trim() + objectInfo.getAddress().toString() + "\n";
        String time = myobjectinfo_detail_time.getText().toString().trim() + objectInfo.getDate().getDate().toString() + "\n";
        String name = "发布人:" + objectInfo.getName().toString() + "\n";
        String phone = "联系电话：" + objectInfo.getPhone().toString() + "\n";
        String qq = "qq:" + objectInfo.getQq().toString() + "\n";
        String content = title + category + describe + address + time + name + phone + qq;
        return content;
    }

    @Override
    public String getShareImageUrl() {
        return objectInfo.getPhoto();
    }
}
